home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / modelers / geomview / source.lha / Geomview / src / lib / gprim / mesh / meshcreate.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-23  |  4.1 KB  |  171 lines

  1. /* Copyright (c) 1992 The Geometry Center; University of Minnesota
  2.    1300 South Second Street;  Minneapolis, MN  55454, USA;
  3.    
  4. This file is part of geomview/OOGL. geomview/OOGL is free software;
  5. you can redistribute it and/or modify it only under the terms given in
  6. the file COPYING, which you should have received along with this file.
  7. This and other related software may be obtained via anonymous ftp from
  8. geom.umn.edu; email: software@geom.umn.edu. */
  9. static char *copyright = "Copyright (C) 1992 The Geometry Center";
  10.  
  11. /* Authors: Charlie Gunn, Stuart Levy, Tamara Munzner, Mark Phillips */
  12.  
  13. #include "meshP.h"
  14.  
  15. static tossmesh(Mesh *);
  16. static int meshfield(int copy, int amount,
  17.         void **fieldp, void *value, char *name);
  18.  
  19. Mesh *
  20. MeshCreate (exist, classp, a_list)
  21. Mesh *exist;
  22. GeomClass *classp;
  23. va_list a_list;
  24. {
  25.     register Mesh *mesh;
  26.     int        attr, copy = 1, fourd = 0;
  27.     int        i;
  28.     int        npts;
  29.     HPoint3    *p;
  30.     Point3     *n, *u, *p3;
  31.     ColorA    *c;
  32.  
  33.     p = NULL; n = NULL; u = NULL; c = NULL; p3 = NULL;
  34.  
  35.     if (exist == NULL) {
  36.     mesh = OOGLNewE(Mesh, "MeshCreate mesh");
  37.     bzero(mesh, sizeof(Mesh));
  38.     GGeomInit (mesh, classp, MESHMAGIC, NULL);
  39.     mesh->flag = 0;
  40.     mesh->nu = 1;
  41.     mesh->nv = 1;
  42.     mesh->umin = -1;
  43.     mesh->umax = -1;
  44.     mesh->vmin = -1;
  45.     mesh->vmax = -1;
  46.     } else {
  47.     /* Check that exist is a Mesh... */
  48.     mesh = exist;
  49.     }
  50.  
  51.     npts = mesh->nu * mesh->nv;
  52.  
  53.     while (attr = va_arg (a_list, int)) switch (attr) {
  54.     case CR_FLAG:
  55.         mesh->flag = va_arg (a_list, int);
  56.         break;
  57.     case CR_NU:
  58.         mesh->nu = va_arg (a_list, int);
  59.         tossmesh(mesh);
  60.         npts = mesh->nu * mesh->nv;
  61.         break;
  62.     case CR_NV:
  63.         mesh->nv = va_arg (a_list, int);
  64.         tossmesh(mesh);
  65.         npts = mesh->nu * mesh->nv;
  66.         break;
  67.     case CR_UMIN:
  68.         mesh->umin = va_arg (a_list, int);
  69.         break;
  70.     case CR_UMAX:
  71.         mesh->umax = va_arg (a_list, int);
  72.         break;
  73.     case CR_VMIN:
  74.         mesh->vmin = va_arg (a_list, int);
  75.         break;
  76.     case CR_VMAX:
  77.         mesh->vmax = va_arg (a_list, int);
  78.         break;
  79.     case CR_POINT:
  80.         if(mesh->p) OOGLFree(mesh->p);
  81.         mesh->p = OOGLNewNE(HPoint3, npts, "mesh points");
  82.         p3 = va_arg(a_list, Point3 *);
  83.         Pt3ToPt4(p3, mesh->p, npts);
  84.         if(!copy) OOGLFree(p3);
  85.         break;
  86.  
  87.     case CR_POINT4:
  88.         meshfield(copy, npts*sizeof(HPoint3), (void **)&mesh->p,
  89.         (void *)va_arg (a_list, HPoint3 *), "mesh points");
  90.         break;
  91.  
  92.     case CR_NORMAL:
  93.         mesh->flag = (mesh->flag & ~MESH_N) |
  94.         (MESH_N & meshfield(copy, npts*sizeof(Point3),
  95.                 (void **)&mesh->n,
  96.                 (void *)va_arg (a_list, Point3 *),
  97.                 "mesh normals"));
  98.         break;
  99.  
  100.     case CR_U:
  101.         mesh->flag = (mesh->flag & ~MESH_U) |
  102.         (MESH_U & meshfield(copy, npts*sizeof(Point3),
  103.                 (void **)&mesh->u,
  104.                 (void *)va_arg (a_list, Point3 *),
  105.                 "mesh texture coords"));
  106.         break;
  107.  
  108.     case CR_COLOR:
  109.         mesh->flag = (mesh->flag & ~MESH_C) |
  110.         (MESH_C & meshfield(copy, npts*sizeof(ColorA),
  111.                 (void **)&mesh->c,
  112.                 (void *)va_arg (a_list, ColorA *),
  113.                 "mesh colors"));
  114.         break;
  115.  
  116.     default:
  117.         if (GeomDecorate (mesh, ©, attr, &a_list)) {
  118.         GeomError (0, "MeshCreate: Undefined option: %d", attr);
  119.         OOGLFree (mesh);
  120.         return NULL;
  121.         }
  122.     }
  123.     /* set submesh dimensions if not otherwise set */
  124.     if (mesh->umin == -1)    mesh->umin = 0;
  125.     if (mesh->umax == -1)    mesh->umax = mesh->nu-1;
  126.     if (mesh->vmin == -1)    mesh->vmin = 0;
  127.     if (mesh->vmax == -1)    mesh->vmax = mesh->nv-1;
  128.  
  129.     return mesh;
  130. }
  131.  
  132. static
  133. tossmesh(register Mesh *m)
  134. {
  135.     if(m->p) OOGLFree(m->p);
  136.     if(m->n) OOGLFree(m->n);
  137.     if(m->c) OOGLFree(m->c);
  138.     if(m->u) OOGLFree(m->u);
  139.     if(m->d) OOGLFree(m->d);
  140.     if(m->nd) OOGLFree(m->nd);
  141.     m->p = NULL;
  142.     m->n = NULL;
  143.     m->c = NULL;
  144.     m->u = NULL;
  145.     m->d = NULL;
  146.     m->nd = NULL;
  147.     m->umin = m->umax = m->vmin = m->vmax = -1;
  148. }
  149.  
  150. static int
  151. meshfield(int copy, int amount, void **fieldp, void *value, char *name)
  152. {
  153.     if(value) {
  154.     if(copy) {
  155.         if(*fieldp == NULL)
  156.         *fieldp = OOGLNewNE(char, amount, name);
  157.         memcpy(*fieldp, value, amount);
  158.     } else {
  159.         if(*fieldp)
  160.         OOGLFree(*fieldp);
  161.         *fieldp = value;
  162.     }
  163.     return ~0;
  164.     } else {
  165.     if(*fieldp)
  166.         OOGLFree(*fieldp);
  167.     *fieldp = NULL;
  168.     return 0;
  169.     }
  170. }
  171.